<?php 
require_once 'userManagement.php';
require_once 'restaurantManagement.php';
require_once 'profits.php';
require_once '../inc/database/DataBase.php';
require_once '../config.php';
require_once 'Settings.php';
$db = new DataBase();
$config = new Config();
$db->connect($config -> getHost(), $config -> getPort(), $config->getDbname(), $config->getUser(), $config->getPass());
$iterator=null;
$statement=null;
?>
<!DOCTYPE html>
<html>
<head>
  <title>Panel Administratora</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8; width=device-width, initial-scale=1.0" >
  <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<?php
if (isset($_GET['sM'])) {	  
  if($_GET['sM']==1){
?>
	<div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-warning">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-danger">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a href="../" type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
		    <a href="settingsManagement.php?sM=1" type="button" class="btn btn-danger btn-block" style="width: 270px;">Zmienne globalne</a>
			<a href="settingsManagement.php?sM=2" type="button" class="btn btn-warning btn-block" style="width: 270px;">Edytuj zmienną globalną</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-inline" role="form" action="settingsManagement.php?sM=1" method="POST">
			  <legend><center>Dodaj nową zmienną</center></legend>
			  <center>
		        <div class="form-group">
                  <input type="text" class="form-control" name="settingName" placeholder="Nazwa Zmiennej">
                </div>
			    <div class="form-group">
                  <input type="text" class="form-control" name="settingValue" placeholder="Wartość zmiennej">
                </div>
                <button type="submit" name="addSettingButton" class="btn btn-primary">Dodaj!</button>
			  </center>
            </form>
<?php
	if(isset($_REQUEST['addSettingButton'])) {
		if(isset($_POST['settingName'])){
			if(isset($_POST['settingValue'])){
				$setting= new Setting();
				$setting->setName($_POST['settingName']);
				$setting->setValue($_POST['settingValue']);
				$db->exec('insert into settings (name, value) values ("'.$setting->getName().'", "'.$setting->getValue().'")');
			}
		}
	}
?>
			<br>
			<form class="form-group" style="height: 290px">
			  <legend><center>Zmienne istniejące już w bazie:</center></legend>
			  <label class="col-sm-9 control-label">Nazwa zmiennej</label>
			  <label class="col-sm-3 control-label"><center>Wartość Zmiennej</center></label><br><br>			  
<?php
$statement=$db->query('select name, value from settings');
foreach($statement as $row)
      {
          echo('<label class="col-sm-9 control-label">'.$row['name'].'</label>');
		  echo('<label class="col-sm-3 control-label"><center>'.$row['value'].'</center></label><br>');
      }
      $statement->closeCursor();
?>
			  </label>
			</form>
		  </div>
		</div>
	</div>
  </div>
</body>
</html>
<?php
  }
  else{
?>
  <div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-warning">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-danger">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a href="" type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
		    <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning btn-block" style="width: 270px;">Zmienne globalne</a>
			<a href="settingsManagement.php?sM=2" type="button" class="btn btn-danger btn-block" style="width: 270px;">Edytuj zmienną globalną</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-inline" role="form" action="settingsManagement.php?sM=2" method="POST">
			  <legend><center>Edytuj zmienną</center></legend>
			  <center>
				<div class="form-group">
                  <select style="height:32px" name="editSettingName">
<?php
$statement=$db->query('select name from settings');
foreach($statement as $row)
      {
?>
					<option>
<?php
          echo($row['name'].'</option>');					
      }
      $statement->closeCursor();
?>	
			      </select>
                </div>
				<div class="form-group">
                  <input type="text" class="form-control" placeholder="Nazwa Zmiennej" name="editSettingValue">
                </div>
                <button type="submit" class="btn btn-primary" name="editButton">Edytuj!</button>
				<button type="submit" class="btn btn-primary" name="deleteButton">Usuń!</button>
			  </center>
            </form>
<?php
	if(isset($_REQUEST['editButton'])) {
		if(isset($_POST['editSettingName'])){
			if(isset($_POST['editSettingValue'])){
				$setting= new Setting();
				$setting->setName($_POST['editSettingName']);
				$setting->setValue($_POST['editSettingValue']);
				$db->exec('update settings set value='.$setting->getValue().' where name="'.$setting->getName().'"');
			}
		}
	}
	if(isset($_REQUEST['deleteButton'])) {
		if(isset($_POST['editSettingName'])){
			$db->exec('delete from settings where name="'.$_POST['editSettingName'].'"');
		}
	}
?>
		  </div>
		</div>
	</div>
  </div>
  </body>
  </html>
<?php
  }
}
?>